package com.android.support.lib.universal.sdk.weibo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.TextUtils;

import android.view.WindowManager;
import android.widget.Toast;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.general.Session;
import com.android.support.lib.universal.listenner.BaseListenner;
import com.android.support.lib.universal.sdk.weibo.general.AccessTokenKeeper;
import com.android.support.lib.universal.sdk.weibo.general.Constants;
import com.android.support.lib.universal.sdk.weibo.models.ErrorInfo;
import com.android.support.lib.universal.sdk.weibo.models.User;
import com.android.support.lib.universal.sdk.weibo.openapi.UsersAPI;
import com.android.support.lib.universal.view.dialog.CustomProgressDialog;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;


/**
 * 该类主要演示了如何使用微博 OpenAPI 来获取以下内容： <li>获取用户信息 <li>通过个性域名获取用户信息 <li>
 * 批量获取用户的粉丝数、关注数、微博数
 * 使用说明：(微博授权成功后，微博用户信息保存在Session里。)
 * User user= (User) Session.getSession().get("User");
 * if(user!=null){
 * //微博授权成功
 * }
 * 设置回调监听
 * WBUserAPIActivity.setListenner(new BaseListenner<com.android.support.lib.universal.sdk.weibo.models.User>() {});
 */
public class WBUserAPIActivity extends Activity {
    private CustomProgressDialog progressDialog;
    /**
     * 当前 Token 信息
     */
    private Oauth2AccessToken mAccessToken;
    /**
     * 用户信息接口
     */
    private UsersAPI mUsersAPI;

    //监听接口
    public static BaseListenner<User> listenner;

    //设置回调监听接口
    public static void setListenner(BaseListenner<User> listenner) {
        WBUserAPIActivity.listenner = listenner;
    }

    /**
     * @see {@link Activity#onCreate}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //手机SDK版本要求在4.4及以上
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            // 透明状态栏.这样状态栏的颜色就会变的和背景色一样
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.lib_activity_sdk_weibo);

        progressDialog = CustomProgressDialog.createDialog(this);
        progressDialog.show();

        // 获取当前已保存过的 Token
        mAccessToken = AccessTokenKeeper.readAccessToken(this);
        // 获取用户信息接口
        mUsersAPI = new UsersAPI(this, Constants.APP_KEY, mAccessToken);

        if (mAccessToken != null && mAccessToken.isSessionValid()) {
            // 获取用户信息
            // String uid = mAccessToken.getUid();
            long uid = Long.parseLong(mAccessToken.getUid());
            mUsersAPI.show(uid, mListener);

            // 取用户的粉丝数、关注数、微博数
            //long[] uids = { Long.parseLong(mAccessToken.getUid()) };
            //mUsersAPI.counts(uids, mListener);

        } else {
            Toast.makeText(WBUserAPIActivity.this,
                    "Access Token 不存在，请先登录",
                    Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 微博 OpenAPI 回调接口。
     */
    private RequestListener mListener = new RequestListener() {
        /**
         * 完成
         */
        @Override
        public void onComplete(String response) {
            if (!TextUtils.isEmpty(response)) {
                // 调用 User#parse 将JSON串解析成User对象
                User user = User.parse(response);
                if (user != null) {
                    //用户获取成功,存储微博用户
                    Session.getSession().put("User", user);
                    if(listenner!=null){
                        listenner.onSuccess(user);
                    }
                } else {
                    Toast.makeText(WBUserAPIActivity.this, response,
                            Toast.LENGTH_LONG).show();
                }
            }
            progressDialog.dismiss();
            WBUserAPIActivity.this.finish();
        }

        /**
         * 异常
         */
        @Override
        public void onWeiboException(WeiboException e) {
            ErrorInfo info = ErrorInfo.parse(e.getMessage());
            Toast.makeText(WBUserAPIActivity.this, info.toString(),
                    Toast.LENGTH_LONG).show();
            progressDialog.dismiss();
            WBUserAPIActivity.this.finish();
        }
    };
}
